package com.HouseManage.utils;

import com.HouseManage.beans.House;

import java.sql.*;
import java.util.ResourceBundle;

public class JdbcUtils {
     private ResourceBundle bundle =  ResourceBundle.getBundle("resource/db");
     private String url = bundle.getString("url");
     private String user  ;
     private String password ;
     private String driver;
     String strings = new String();
     Connection con;
     Statement stt;

    public Connection getCon() {
        try{
            password = bundle.getString("password");
            user = bundle.getString("user");
            this.con = DriverManager.getConnection(url,user,password);
            return con;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }
//记得调用实用完后close

    public void test(){
        System.out.println(getCon());
    }

    public String addMessage(House house){
        try{
            int o = 0;
            System.out.println(house.status);
            this.con = getCon();
            stt = con.createStatement();
            String name = house.getName();
            String year = house.getYear();
            String price = house.getPrice();
            String tips = house.getTips();
            String insert = "insert into housemanage(name,year,tips,price,state ) value ('"+name+"',"+year+","+tips+","+price+","+o+")";
            int i =stt.executeUpdate(insert);
            if(i==1){
                strings = "添加成功";
            }else strings = "添加失败";
            stt.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return strings;
    }
//传入一个House对象，录入房子信息

    public String delMessage(House house){
        try{
            this.con = getCon();
            stt = con.createStatement();
            String name = house.getName();
            String tips = house.getTips();
            String insert = "delete from housemanage where name = '"+name+"'"+"and tips = "+tips+"";
            int i = stt.executeUpdate(insert);
            if(i==1){
                strings = "删除成功";
            }else
                strings = "删除失败";
            stt.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return strings;
    }

//删除房间信息，name和tips同时符合的项目可以删除

    public String searchMessage(House house){
        try{
            String state;
            this.con = getCon();
            String name = house.getName();
            String tips = house.getTips();
            String insert = "select * from housemanage";
            stt = con.createStatement();
            ResultSet rs = stt.executeQuery(insert);
            while (rs.next()){
                if(rs.getString("name").equals(name)){
                    int o = (int) rs.getInt("state");
                    if(o==1){
                        state = "出租";
                    }
                    else {
                        state = "无人";
                    }
                    if(rs.getString("tips").equals(house.getTips())){
                        strings = house.getName()+house.getTips()+"号：该房使用了" + rs.getInt("year") + "年，" + "处于" + state + "状态,价格" + rs.getInt("price") + "元";
                    }
                }
                if(!rs.next()){
                    strings = "无此房间";
                }
            }
            con.close();
            stt.close();
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return strings;
    }
//查询房间，传入house，根据需求修改

    public String setMessage(House house){
        try{
            int i = house.status?1:0;
            this.con = getCon();
            stt = con.createStatement();
            String insert = "update housemanage set year = "+house.getYear()+" where tips = "+house.getTips()+" and name = '"+house.getName()+"' ";
            stt.executeUpdate(insert);
            insert = "update housemanage set price = "+house.getYear()+" where tips = "+house.getTips()+" and name = '"+house.getName()+"' ";
            int count = stt.executeUpdate(insert);
            insert = "update housemanage set state = "+i+" where tips = "+house.getTips()+" and name = '"+house.getName()+"' ";
            count = stt.executeUpdate(insert);
            if(count == 1)
                strings ="更改成功";
            else
                strings = "更改失败";
            con.close();
            stt.close();
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return strings;
    }

    public JdbcUtils() {
        try{
            driver = bundle.getString("driver");
            Class.forName(driver);
        }catch (ClassNotFoundException e){
            System.out.println(e);
        }
    }

}
